package com.tanhua.controller;

import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.SettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;

    @GetMapping("/settings")
    public ResponseEntity<SettingsVo> getSettings() {
        SettingsVo vo = settingsService.getSettings();
        return ResponseEntity.ok(vo);
    }

    @PostMapping("/questions")
    public ResponseEntity<Void> setQuestions(@RequestBody Map map) {
        String content = map.get("content").toString();
        Long userId = UserHolder.getUserId();
        settingsService.updateQuestions(content, userId);
        return ResponseEntity.ok().body(null);
    }

    @PostMapping("/notifications/setting")
    public ResponseEntity<Void> updateNotification(@RequestBody Settings settings) {
        Long userId = UserHolder.getUserId();
        settings.setUserId(userId);
        settingsService.updateNotification(settings);
        return ResponseEntity.ok().body(null);
    }

    @GetMapping("/blacklist")
    public ResponseEntity<PageResult> getBlackList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pagesize) {
        PageResult pageRequest = settingsService.getBlackList(page, pagesize);
        return ResponseEntity.ok().body(pageRequest);
    }

    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity<Void> deleteBlackList(@PathVariable("uid") Long blackUserId) {
        settingsService.deleteBlackList(blackUserId);
        return ResponseEntity.ok().body(null);
    }


}
